package edu.byu.intex2.data.bo;

import edu.byu.intex2.data.DataException;
import edu.byu.intex2.data.dao.ManufacturerDAO;
import edu.byu.intex2.data.dao.PartDAO;

/**
 * Tracks each part used in a service
 * @author Brady White
 */
public class Part extends BusinessObject
{
    private String name;
    private String partNumber;
    private double price;
    
    /** Manufacturer */
    private String manufacturerId;
    private Manufacturer manufacturer;

    /** Creates a new instance of BusinessObject */
    public Part(String id)
    {
        super(id);
    }//constructor

    /** Convenience method to call the correct DAO to save this business object */
    public void save() throws DataException {
        try {
            PartDAO.getInstance().save(this);
        } catch (DataException e) {
            e.printStackTrace();
        }
    }

    /**
     * @return the costOfLabor
     */
    public String getPartNumber()
    {
        return partNumber;
    }

    /**
     * @param costOfLabor the costOfLabor to set
     */
    public void setPartNumber(String partNumber)
    {
        this.partNumber = partNumber;
        setDirty(true);
    }

    /**
     * @return the costOfParts
     */
    public double getPrice()
    {
        return price;
    }

    /**
     * @param costOfLabor the costOfLabor to set
     */
    public void setPrice(double partPrice)
    {
        this.price = partPrice;
        setDirty(true);
    }
    /**
     * @return the name
     */
    public String getName()
    {
        return name;
    }

    /**
     * @param name
     */
    public void setName(String name)
    {
        this.name = name;
        setDirty(true);
    }


    /**
     * @return the manufacturerId
     */
    public String getManufacturerId() {
        return manufacturerId;
    }

    /**
     * @param manufacturerId the manufacturerId to set
     */
    public void setManufacturerId(String manufacturerId) {
        this.manufacturerId = manufacturerId;
        setDirty(true);
    }

    /**
     * @return the manufacturer from the DAO
     */
    public Manufacturer getManufacturer() {
        if (manufacturer == null) {
            try {
                manufacturer = ManufacturerDAO.getInstance().read(manufacturerId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return manufacturer;
    }

    /**
     * @param manufacturer the manufacturer to set
     */
    public void setManufacturer(Manufacturer manufacturer) {
        this.manufacturer = manufacturer;
        this.setManufacturerId( manufacturer.getId());
        setDirty(true);
    }

    @Override
    public String toString(){
        return getName();
    }
}
